---- UNIT TESTING

on check res,expected
  if (res<>expected) then
    put "test - failed"
  else
    put "test OK"
  end if
end

on sum me,a1,a2,a3,a4
  return a1+a2+a3+a4
end
---------------
-- SHORTCUTS --
---------------
on var n
  return LingoF().newVar()
end

on fun
  o = LingoF().getShortcut(#fun,the paramcount)
  repeat while o.process(param(o.pindex))
  end repeat
  return o.getValue() 
end

on $
  o = LingoF().getShortcut(#$,the paramcount)
  repeat while o.process(param(o.pindex))
  end repeat
  return o.getValue()
end

on !
  o = LingoF().getShortcut(#!,the paramcount)
  repeat while o.process(param(o.pindex))
  end repeat
  return o.getValue()
end

on _
  return LingoF().getLambda(#LingoF,#Skip, the paramcount , param(1) )
end

on @
  return LingoF().getLambda(#LingoF,#Compose, the paramcount , param(1) )
end



---------------
on startTesting
  
on simpleTest
  plus = LingoF().getLambda(#Lingo,#op_plus )
  return { plus[2][3] , 5}
end


on skipOperator
  f = $(#-)._[6]
  return {f[10],4}
end

on LingoHandlerAsFunction
  f = fun(#sum , script "LingoF-UnitTests",4)
  return { f[2][3][12][6] , 23 }
end

on LingoMovieHandlerAsFunction
  f = fun(#TestMovieFunctionSum , _movie,15)
  return { f[2][3][12][6][7][1][1][1][1][1][1][1][1][1][11] , 50 }
end

on LingoMovieHandlerAsFunctionWithMoreArguments
  f = fun(#TestMovieFunctionSum , _movie,25)
  return { f[2][3][12][6][7][1][1][1][1][1][1][1][1][1][11][2][2][2][2][2][1][1][1][1][1] , 65 }
end

on AnonymousLingoExpression
  f = fun("x,y:x+y")
  return{  f[2][4] , 6}
end

on AnonymousLingoExpressionWithNamedContext
  f = fun("x,y:x+y + t.length",[#t:"Hello" ])
  return{  f[2][4] , 11}
end

on AnonymousLingoSentencesWithContext
  f = fun("x,y","return x + y + _1 ",[2])
  return{  f[10][3] , 15 }
end

on AnonymousLingoSentencesWithNamedContext
  f = fun("x,y","return x + y + z + t.length",[ #z:5 , #t:"Hello" ])
  return{  f[10][3] , 23 }
end


on ConstantBoundedExpression1
  x = Var()
  f = fun(x)[3]
  return{  f[6]    , 3}
end


on BoundedExpression1
  x = Var()
  f = fun(x)[$(#-80) [x] ]
  return{  f[6]    , 74}
end

on BoundedExpression2
  x = Var()
  f = fun(x)[$(#-x) [80]  ]
  return{  f[86]   ,  6}
end

on BoundedExpression3
  x = Var()
  f = fun(x)  [ $(#*) [x[5][2]] [$(#+) [2] [3]] ]
  return{  f[$(#-)]   ,  15}
end

on BoundedExprWithMultipleApplication
  x = Var()
  f = fun(x) [   $(#+)  [$(#-)[x][6]]  [ $(#*)[3][x] ]     ]
  return{ f [10]   , 34 }
end

on BoundedExprWithMultipleApplication2
  len = fun("x: x.length")
  takeFirst = LingoF().open(#Lists).take[1]
  x = Var()
  f = fun(x) [  $(#proplist, #length, len[x], #initial, takeFirst[x]) ]
  return { f["hello"]  , [#length: 5, #initial: "h"] }
end

on BoundedExprWithMultipleApplicationNested
  x = Var()
  f = fun(x) [   $(#+)  [$(#-)[x][ $(#*)[3][x] ] ]  [100]     ]
  return{ f [10]   , 80 }
end

on BoundedExprWithMultipleApplicationNestedAndCurried
  x = Var()
  f = fun(x) \
      [   $(#+)  \
               [$(#-) \
                    [ x] \
                    [ $(#*)[3][x] ] \
               ]     \
      ]
  return{ f [10]   [100] , 80 }
end

on BoundedExprWithMultipleApplicationNestedAndCurriedAndSkipped
  x = Var()
  f = fun(x) \
  [   \
        $(#+) \
                 [$(#-)._[$(#*)[3][x]] [x] ]    \
  ]
  return{ f [10]   [100] , 80 }
end

on MultiVarBoundedExpressions
  x = Var()
  y = Var()
  len = fun("x:x.length")
  return { fun(y) [ fun(x) [ x[y] ] [len] ] ["9600"]  , 4}
end

on MultiVarBoundedExpressions2
  x = Var()
  y = Var()
  
  return { fun(y) [ fun(x) [ $(#+x)[$(#+y)[x]]  ] [10]  ]  [38]    , 58 }
end

on AnonymousRecursion
  f = Var()
  n = var()
  rec = LingoF().open(#lingoF).rec
  
  fact = rec  [ fun(f,n)                      \
                        .|( $(#=0)[n] , 1)  \
                        .|( true      , $(#*n) [f[$(#-,n,1)]] ) ]
  return { fact[5] , 120 }
end

on flipOperator
  x = Var()
  $ = LingoF().Open()
  f = $.fun(x) [ $.flip [x] [3] [5] ]
  return { f [ $(#-) ] , 2}
end

on fLists
  $ = LingoF().Open(#Lists)
  x = Var()
  f = fun(x) [ $.map  [$(#+) [1]]  [ $(#List) [!(3,x,5)] ] ]
  return {f[70] , [4, 71,6]}
end

on fLists_CompactVersion
  $ = LingoF().Open(#Lists)
  x = Var()
  f = fun(x) [ $.map  [$(#+1)]  [ $(#List,3,x,5)] ]
  return {f[70] , [4, 71,6]}
end

on functionLists
  lst = [ "This is" ,  "LingoF" ,  "Functional Lingo Framework" ]
  $ = LingoF().Open(#Lists)
  x = Var()
  
  fnList = fun(x) [ $(#PropList , #Length , $(#length,x)  , #FirstLetter , $.take[1][x] )  ]
  results =  $.map  [fnList]    [lst]
  return {results , [[#length: 7, #FirstLetter: "T"], [#length: 6, #FirstLetter: "L"], [#length: 26, #FirstLetter: "F"]]}
end

on List_filter
  x = Var()
  $ = LingoF().Open(#Lists)
  lst = { [#name: "Richard" , # score : 9 ] ,[#name: "Fred" , # score : 3 ] ,[#name: "John" , # score : 5 ] }
  results = $.filter[ fun(x)  [ $(#=5) [x[#score]] ]  ] [lst]
  return {results[1].name , "John"}
end

on List_filter2
  x = Var()
  y = Var()
  $ = LingoF().Open(#Lists)
  lst = { [#name: "Richard" , # score : 9 ] ,[#name: "Fred" , # score : 3 ] ,[#name: "John" , # score : 5 ] }
  results = $.filter[ fun(y,x)  [ $(#=5) [x[y] ]] [#score]]   [lst]
  return {results[1].name , "John"}
end

on List_filter_free_point_version
  $ = LingoF().Open(#Lists)
  lst = { [#name: "Richard" , # score : 9 ] ,[#name: "Fred" , # score : 3 ] ,[#name: "John" , # score : 5 ] }
  results = $.filter[ $(#=5).@[$(#getat)._[#score]] ] [lst]
  return {results[1].name , "John"}
end


on shortcut1
  return { $(#getAt) [{4,7,0,34,756,978}][5] , 756}
end

on shortcut2
  return { $(#getAt,{4,7,0,34,756,978}) [5] , 756}
end

on shortcut3
  return { $(#getAt,{4,7,0,34,756,978},5) , 756}
end

on shortcut4
  return { $(#proplist) [!(23,54,76,90)] , [23: 54, 76: 90]}
end

on shortcut5
  return { $(#proplist,23,54,76,90) , [23: 54, 76: 90]}
end

on shortcut6
  return { $() [$(#+)][12][34] , 46}
end

on shortcut7
  return { $($(#+))[12][34] , 46}
end

on shortcut8
  return { $($(#+),12)[34] , 46}
end

on shortcut9
  return { $($(#+),12,34) , 46}
end

on shortcut10
  return { $(#+12)[34] , 46}
end


on JSFunction
  plus = fun( GetPlusFunction() )
  $ = LingoF().open(#Lists)
  return {$.map[plus[2]] [ [10,20,30] ] , [12,22,32]}
end

on LingoFInJs
  return {MapTestInJS(), [12,22,32]}
end

-- List library
